package cn.tedu._05weibo.controller;

import cn.tedu._05weibo.common.response.JsonResult;
import cn.tedu._05weibo.common.response.StatusCode;
import cn.tedu._05weibo.mapper.WeiboMapper;
import cn.tedu._05weibo.pojo.dto.WeiboDTO;
import cn.tedu._05weibo.pojo.entity.Weibo;
import cn.tedu._05weibo.pojo.vo.UserVO;
import cn.tedu._05weibo.pojo.vo.WeiboDetailVO;
import cn.tedu._05weibo.pojo.vo.WeiboIndexVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Validated
@Api(tags = "微博模块")
@RestController
@RequestMapping("/v1/weibo")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;
//    发布微博
    @ApiOperation("发布微博")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody WeiboDTO weiboDTO , @ApiIgnore HttpSession session){
        UserVO userVo = (UserVO)session.getAttribute("user");
        System.out.println("weiboDTO = " + weiboDTO + ", session = " + userVo);
        if (userVo==null){
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
        Weibo weibo=new Weibo();
        BeanUtils.copyProperties(weiboDTO,weibo);
        weibo.setCreated(new Date());
        weibo.setUserId(userVo.getId());
        weiboMapper.insertWeibo(weibo);
        return JsonResult.ok(weibo);
    }

//    微博列表
    @ApiOperation("微博列表")
    @GetMapping("selectIndex")
    public JsonResult selectIndexWeibo(){
        return JsonResult.ok(weiboMapper.selectIndex());
    }

//    微博详情
    @ApiOperation("微博详情")
@GetMapping("selectById")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(  name = "id", value = "微博ID",
                    required = true, dataType = "Long"),

            @ApiImplicitParam(  name = "username",value = "用户名",
                    required = true,dataType = "Long")
    })
public JsonResult weiboDetailVO(    @Range(min = 200,max = 300,message = "微博ID必须在200-300之间")
                                    @RequestParam Integer id,String username){
//public JsonResult weiboDetailVO(@Range(min = 200,max = 300,message = "微博ID必须在200-300之间") Integer id) {
        if (id < 0) {
            throw new IllegalArgumentException("ID值不能小于0");
        }
        WeiboDetailVO weiboDetailVO = weiboMapper.selectWeiboDetailVO(id);
        return JsonResult.ok(weiboDetailVO);
    }


}
